/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.qestudio.client.swing.view.components.registries;

import java.util.List;

import javax.swing.table.AbstractTableModel;

import ar.com.qestudio.core.model.Note;
import ar.com.qestudio.core.model.Registry;

/**
 *
 * @author EP30873479
 */
public class DefaultList extends javax.swing.JPanel implements Tabbable {

    /**
     * Creates new form DefaultList
     */
    public DefaultList() {
        initComponents();
    }

    public DefaultList(List<Registry> registries) {
        initComponents();
        jTable1.setModel(new JTableModelTransient(registries));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setLayout(new java.awt.BorderLayout());

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(jTable1);

        add(jScrollPane2, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

    public javax.swing.JScrollPane getjScrollPane2() {
        return jScrollPane2;
    }

    public void setjScrollPane2(javax.swing.JScrollPane jScrollPane2) {
        this.jScrollPane2 = jScrollPane2;
    }

    public javax.swing.JTable getjTable1() {
        return jTable1;
    }

    public void setjTable1(javax.swing.JTable jTable1) {
        this.jTable1 = jTable1;
    }

    @Override
    public Registry getSelected() {
        return ((JTableModelTransient)jTable1.getModel()).getIdByRow(jTable1.getSelectedRow());
    }
    
    public class JTableModelTransient extends AbstractTableModel {

        private static final long serialVersionUID = -1684575201953195767L;
        
        // Two arrays used for the table data
        private String[] columnNames = {"ID", "INTERESADO", "FECHA CREACION", "DESCRIPCION", "FECHA MODIFICACION", "ADJUNTOS", "USUARIO"};
        private List<Registry> registros;
        private Object[][] data;

        public JTableModelTransient(List<Registry> registros) {
            this.registros = registros;
            data = new Object[registros.size()][3];
            for (int i = 0; i < registros.size(); i++) {
                String descripciones = new String();
                for (Note note : registros.get(i).getNotes()) {
                    descripciones += note.getDescription();
                }
                data[i] = new Object[]{registros.get(i).getId(), registros.get(i).getInterested().getName(), registros.get(i).getCreationDate(), descripciones, registros.get(i).getModifyDate(), registros.get(i).getAttachments().size(), "Damian"};
            }
        }

        public Registry getIdByRow(Integer row) {
            return registros.get(row);
        }

        @Override
        public int getRowCount() {
            return registros.size();
        }

        @Override
        public int getColumnCount() {
            return columnNames.length;
        }

        @Override
        public Object getValueAt(int row, int column) {
            return data[row][column];
        }

        // Used by the JTable object to set the column names
        @Override
        public String getColumnName(int column) {
            return columnNames[column];
        }

        // Used by the JTable object to render different
        // functionality based on the data type
        @Override
        @SuppressWarnings({"unchecked", "rawtypes"})
        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

        @Override
        public boolean isCellEditable(int row, int column) {
            if (column == 0 || column == 1) {
                return false;
            } else {
                return true;
            }
        }
    }
}